package com.mdd.admin.service.impl.yylq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.yylq.ITAppCourseService;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.yylq.TAppCourseCreateValidate;
import com.mdd.admin.validate.yylq.TAppCourseSearchValidate;
import com.mdd.admin.validate.yylq.TAppCourseUpdateValidate;
import com.mdd.admin.vo.yylq.TAppCourseDetailVo;
import com.mdd.admin.vo.yylq.TAppCourseListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppCourse;
import com.mdd.common.mapper.yangyangLeadingEnterprise.TAppCourseMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 课程实现类
 * @author LikeAdmin
 */
@Service
public class TAppCourseServiceImpl implements ITAppCourseService {
        
    @Resource
    TAppCourseMapper tAppCourseMapper;

    /**
     * 课程列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TAppCourseListedVo>
     */
    @Override
    public PageResult<TAppCourseListedVo> list(PageValidate pageValidate, TAppCourseSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TAppCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        tAppCourseMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:title:str",
            "=:subtitle:str",
            "=:introduce:str",
            "=:showPic@show_pic:str",
            "=:freeType@free_type:int",
            "=:playCount@play_count:int",
            "=:type:int",
        });

        IPage<TAppCourse> iPage = tAppCourseMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TAppCourseListedVo> list = new LinkedList<>();
        for(TAppCourse item : iPage.getRecords()) {
            TAppCourseListedVo vo = new TAppCourseListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime().getTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime().getTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 课程详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TAppCourse
     */
    @Override
    public TAppCourseDetailVo detail(Integer id) {
        TAppCourse model = tAppCourseMapper.selectOne(
                new QueryWrapper<TAppCourse>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TAppCourseDetailVo vo = new TAppCourseDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 课程新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TAppCourseCreateValidate createValidate) {
        TAppCourse model = new TAppCourse();
        model.setTitle(createValidate.getTitle());
        model.setSubtitle(createValidate.getSubtitle());
        model.setIntroduce(createValidate.getIntroduce());
        model.setShowPic(createValidate.getShowPic());
        model.setFreeType(createValidate.getFreeType());
        model.setPlayCount(createValidate.getPlayCount());
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        model.setType(createValidate.getType());
        tAppCourseMapper.insert(model);
    }

    /**
     * 课程编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TAppCourseUpdateValidate updateValidate) {
        TAppCourse model = tAppCourseMapper.selectOne(
                new QueryWrapper<TAppCourse>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setTitle(updateValidate.getTitle());
        model.setSubtitle(updateValidate.getSubtitle());
        model.setIntroduce(updateValidate.getIntroduce());
        model.setShowPic(updateValidate.getShowPic());
        model.setFreeType(updateValidate.getFreeType());
        model.setPlayCount(updateValidate.getPlayCount());
        model.setUpdateTime(new Date());
        model.setType(updateValidate.getType());
        tAppCourseMapper.updateById(model);
    }

    /**
     * 课程删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TAppCourse model = tAppCourseMapper.selectOne(
                new QueryWrapper<TAppCourse>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tAppCourseMapper.delete(new QueryWrapper<TAppCourse>().eq("id", id));
    }

}
